package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.User;
import com.foreknow.service.impl.RuleServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    RuleServiceImpl ruleServiceImpl;

//一对一双表查询UserName和rule
    @RequestMapping(value = "queryByPage" ,method = RequestMethod.GET)
    public String queryUserWeek(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs =new HashMap<>();
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize =Integer.parseInt((String)  para.get("pageSize"));
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<Rule> rules = ruleServiceImpl.queryUserWeek();
        //PageInfo是PageHelper中内置的分页信息类
        PageInfo<User> info = new PageInfo(rules);
        hs.put("total",info.getTotal());
        hs.put("pageNum",info.getPageNum());
        hs.put("rulelist",rules);
        if (rules.size()>0){
            hs.put("meta",new Constance("获取Rule成功",200));
        }else {
            hs.put("meta",new Constance("获取Rule失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "/addRules" , method = RequestMethod.POST)
    public String addRules(@RequestBody Map<String,Object>para) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        String ruleName=String.valueOf(para.get("ruleName"));
        List<Object> rulesObj = (List<Object>) para.get("rules");
        List<Rule> rules = new ArrayList<>();
        boolean isRight = false;
        if (rulesObj.size()>0 && !ruleName.equals("")) {
            for (Object rule : rulesObj) {
                //设置在反序列化时忽略在JSON字符串中存在，而在Java中不存在的属性
                Rule ruleItem = objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).convertValue(rule, Rule.class);
                ruleItem.setRulename(ruleName);
                rules.add(ruleItem);
            }
            isRight = ruleServiceImpl.addRules(rules);
        }
        if (isRight){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        return objectMapper.writeValueAsString(hs);
    }

}
